/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

using BlindShark;

namespace BlindShark.Widgets
{
	public class Button : IWidget
	{
		public delegate void ClickEvent();
		public ClickEvent Click;
		
		#region Variables
		string _sPicture;
		
		Rectangle _rctButton;
		Surface[] _sfButton;
		
		Color _clrTransparency;
		bool _bTransparent;
		
		bool _bHovered;
		bool _bClicked;
		bool _bHasBeenClicked;
		bool _bEnabled;
		
		string _sFont;
		Sdl.Font _fTextLabel;
		string _sTextLabel;
		Rectangle _rctTextLabel;
		int _iSizeLabel;
		Color _clrTextLabel;
		
		#endregion
		
		#region Accesseurs
		public int X
		{
			get {return _rctButton.X;}
			set {_rctButton.X = value;}
		}
		
		public int Y
		{
			get {return _rctButton.Y;}
			set {_rctButton.Y = value;}
		}
		
		public int Width
		{
			get {return _rctButton.Width;}
		}
		
		public int Height
		{
			get {return _rctButton.Height;}
		}
		
		public int Bottom
		{
			get {return _rctButton.Bottom;}
		}
		
		public bool Enabled
		{
			get {return _bEnabled;}
			set 
			{
				_bEnabled = value;
				if (!_bEnabled)
				{
					_bHovered = false;
					_bClicked = false;
				}
			}
		}
		
		public string Text
		{
			get {return _sTextLabel;}
			set {_sTextLabel = value;}
		}
		
		public string Font
		{
			get {return _sFont;}
			set {_sFont = value;}
		}
		
		public int TextSize
		{
			get {return _iSizeLabel;}
			set {_iSizeLabel = value;}
		}
		
		public Color TextColor
		{
			get {return _clrTextLabel;}
			set {_clrTextLabel = value;}
		}
		
		public bool Transparent
		{
			get {return _bTransparent;}
			set {_bTransparent = value;}
		}
		
		public Color TransparencyColor
		{
			get {return _clrTransparency;}
			set {_clrTransparency = value;}
		}
		
		public bool Hovered
		{
			get {return _bHovered;}
		}
		public bool Clicked
		{
			get {return _bClicked;}
		}
		#endregion
		
		public Button(string pictureFile, bool threeSteps)
		{
			_sPicture = pictureFile;
			_rctButton = new Rectangle(0,0,1,1);
			
			if (threeSteps)
				_sfButton = new Surface[3];
			else
				_sfButton = new Surface[1];
			
			_bHovered = false;
			_bClicked = false;
			_bEnabled = true;
			_sFont = "";
			_sTextLabel = "";
		}
		
		#region IWidget interface members
		public Surface Surface
		{
			get 
			{
				if (_sfButton.Length==3)
				{
					if (_bClicked)
						return _sfButton[2];
					else if (_bHovered)
						return _sfButton[1];
					else
						return _sfButton[0];
				} else
					return _sfButton[0];
			}
		}
		
		public Point Location
		{
			get {return _rctButton.Location;}
			set {_rctButton.Location = value;}
		}
		
		public void Init()
		{
			Surface sfGlobal = SurfaceManager.GetSurface(_sPicture);
			Rectangle rctArea = new Rectangle(0,0,1,1);
			for (int i=0; i<_sfButton.Length; i++)
			{
				rctArea.X = 0;
				rctArea.Y = i * (sfGlobal.Height / _sfButton.Length);
				rctArea.Width = sfGlobal.Width;
				rctArea.Height = sfGlobal.Height / _sfButton.Length;
				_sfButton[i] = sfGlobal.CreateSurfaceFromClipRectangle(rctArea);
				if (_bTransparent)
				{
					_sfButton[i].Transparent = true;
					_sfButton[i].TransparentColor = _clrTransparency;
				}
			}
			
			_rctButton.Size = _sfButton[0].Size;
			
			InitText();
		}
		
		public void CleanUp()
		{
			for(int i=0; i<_sfButton.Length; i++)
				_sfButton[i] = null;
		}
		
		public void HandleEvents()
		{
			if (!_bEnabled)
				return;
			
			Point pm = Mouse.MousePosition;
			_bHovered = _rctButton.Contains(pm);
			_bHasBeenClicked = _bClicked;
			_bClicked = _bHovered && Mouse.IsButtonPressed(MouseButton.PrimaryButton);
			
			if (_bHovered && _bHasBeenClicked && !_bClicked && (Click!=null))
				Click();
		}
		#endregion
		
		private void InitText()
		{
			if (_sFont.Equals("") || _sTextLabel.Equals(""))
				return;
			
			// Initializing the font
			_fTextLabel = new Sdl.Font(_sFont, _iSizeLabel);
			
			// Initializing the text location
			_rctTextLabel.Size = _fTextLabel.SizeText(_sTextLabel);
			_rctTextLabel.X = (_rctButton.Width - _rctTextLabel.Width) / 2;
			_rctTextLabel.Y = (_rctButton.Height - _rctTextLabel.Height) / 2;
			
			// Blitting the text on each surface
			for (int i=0; i<_sfButton.Length; i++)
			{
				_sfButton[i].Blit(_fTextLabel.Render(_sTextLabel, _clrTextLabel),
				                  _rctTextLabel.Location);
			}
		}
	}
}
